package model.events

import model.postgresql.{PgBaseEvent, PgGroupLeave}
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder}
import zio.json.*

case class GroupLeave(
    `object`: GroupLeave.`object`,
    event_id: Option[String],
    timestamp: Option[Long]
) extends BaseEvent(event_id, timestamp) {
  override def convertToJson: String              = this.toJson
  override def getUserId: Some[Long]              = Some(this.`object`.user_id)
  override def setTimestamp(ts: Long): GroupLeave = copy(timestamp = Some(ts))

  override def toPg: Option[PgBaseEvent] =
    for {
      eventId   <- event_id
      timestamp <- timestamp
    } yield PgGroupLeave(
      eventId,
      `object`.self,
      timestamp,
      `object`.user_id
    )
}

object GroupLeave {
  case class `object`(user_id: Long, self: Long)

  given JsonDecoder[`object`] = DeriveJsonDecoder.gen[`object`]
  given JsonEncoder[`object`] = DeriveJsonEncoder.gen[`object`]

  given JsonDecoder[GroupLeave] = DeriveJsonDecoder.gen[GroupLeave]
  given JsonEncoder[GroupLeave] = DeriveJsonEncoder.gen[GroupLeave]
}
